package com.hxh.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.plugins.Page;
import com.hxh.dao.ProjectDao;
import com.hxh.dao.ProjectTableFiledConfigDao;
import com.hxh.entity.Project;
import com.hxh.entity.ProjectTableFiledConfig;
import com.hxh.entity.input.ProjectTableFiledConfigInput;
import com.hxh.entity.po.ClassInfo;
import com.hxh.generater.springBoot.GenerationSpringBootAdapter;
import com.hxh.generater.springBoot.GenerationTableInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
@Transactional
@Slf4j
public class ProjectTableFiledConfigService {
    @Resource
    ProjectTableFiledConfigDao projectTableFiledConfigDao;

    @Autowired
    GenerationSpringBootAdapter generationSpringBootAdapter;

    @Resource
    ProjectDao projectDao;

    private void processAfter(Integer projectId, Integer datasourceId, String tableName) {
        try {
            Project project = projectDao.selectById(projectId);
            ClassInfo classInfo = GenerationTableInfo.getTableInfo(project, datasourceId, tableName);
            generationSpringBootAdapter.generationEntityMappingClass(project, classInfo);
        } catch (Exception e) {
        }

    }

    public List<ProjectTableFiledConfig> selectByIdProjectIdTableNameFiledName(Integer projectId, Integer datasourceId, String tableName) {
        return selectByIdProjectIdTableNameFiledName(projectId, datasourceId, null, tableName);
    }

    public List<ProjectTableFiledConfig> selectByIdProjectIdTableNameFiledName(Integer projectId, Integer datasourceId, String filedName, String tableName) {
        ProjectTableFiledConfigInput projectTableFiledConfig = new ProjectTableFiledConfigInput();
        projectTableFiledConfig.setProjectId(projectId);
        projectTableFiledConfig.setDatasourceId(datasourceId);
        if (StringUtils.isNotBlank(filedName)) {
            projectTableFiledConfig.setFiledName(filedName);
        }
        projectTableFiledConfig.setTableName(tableName);
        return projectTableFiledConfigDao.select(projectTableFiledConfig);
    }

    public List<ProjectTableFiledConfig> select(ProjectTableFiledConfig pojo) {
        ProjectTableFiledConfigInput pojo1 = BeanUtil.copyProperties(pojo, ProjectTableFiledConfigInput.class);
        return select(pojo1);
    }

    public List<ProjectTableFiledConfig> select(ProjectTableFiledConfigInput pojo) {
        return projectTableFiledConfigDao.select(pojo);
    }

    public ProjectTableFiledConfig selectById(Integer id) {
        if (id == null) {
            return null;
        }
        return projectTableFiledConfigDao.selectById(id);
    }

    public void listPage(Page<ProjectTableFiledConfig> page, ProjectTableFiledConfigInput pojo) {
        page.setRecords(projectTableFiledConfigDao.listPage(page, pojo));
    }


    public boolean insert(ProjectTableFiledConfig pojo) {
        boolean res = projectTableFiledConfigDao.insert(pojo) > 0;
        if (res) {
            //插入配置表，触发Mapping生成
            processAfter(pojo.getProjectId(), pojo.getDatasourceId(), pojo.getTableName());
        }
        return res;
    }

    public boolean deleteById(Integer id) {
        ProjectTableFiledConfig projectTableFiledConfig = projectTableFiledConfigDao.selectById(id);
        boolean res = projectTableFiledConfigDao.deleteById(id) > 0;
        if (res) {
            //插入配置表，触发Mapping生成
            processAfter(projectTableFiledConfig.getProjectId(), projectTableFiledConfig.getDatasourceId(), projectTableFiledConfig.getTableName());
        }
        return res;
    }

    public boolean update(ProjectTableFiledConfig pojo) {
        boolean res = projectTableFiledConfigDao.update(pojo) > 0;
        if (res) {
            //插入配置表，触发Mapping生成
            processAfter(pojo.getProjectId(), pojo.getDatasourceId(), pojo.getTableName());
        }
        return res;
    }

}
